/*-------------------------------------------------------------------------
 *
 * md.h
 *	  magnetic disk storage manager public interface declarations.
 *
 *
 * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
 * Portions Copyright (c) 1994, Regents of the University of California
 *
 * src/include/storage/md.h
 *
 *-------------------------------------------------------------------------
 */
#ifndef MD_H
#define MD_H

#include "storage/block.h"
#include "storage/relfilenode.h"
#include "storage/smgr.h"
#include "storage/sync.h"
#include "port/atomics.h"
#include "storage/buffer.h"

typedef size_t KPHC_CFS_STORAGE_TYPE;

#define KPHC_CMP_BYTE_CONVERT_LEN 1
#define KPHC_CMP_DIFF_CONVERT_LEN 1
#define KPHC_CMP_PRE_CHUNK_LEN 3
#define KPHC_CMP_LEVEL_SYMBOL_LEN 1

#define KPHC_CMP_LEVEL_LEN 5
#define KPHC_CMP_ALGORITHM_LEN 3
#define KPHC_CMP_CHUNK_SIZE_LEN 2

const unsigned KPHC_CMP_BYTE_CONVERT_INDEX = 0;
const unsigned KPHC_CMP_DIFF_CONVERT_INDEX = 1;
const unsigned KPHC_CMP_PRE_CHUNK_INDEX = 2;
const unsigned KPHC_CMP_COMPRESS_LEVEL_SYMBOL = 3;
const unsigned KPHC_CMP_LEVEL_INDEX = 4;
const unsigned KPHC_CMP_ALGORITHM_INDEX = 5;
const unsigned KPHC_CMP_CHUNK_SIZE_INDEX = 6;

typedef struct CmpBitStuct {
    const unsigned int bitLen;
    const unsigned int mask;
    const unsigned int moveBit;
}KPHCCmpBitStuct;

const KPHCCmpBitStuct gCmpBitStruct[] = {{KPHC_CMP_BYTE_CONVERT_LEN, 0x01, 15},
    {KPHC_CMP_DIFF_CONVERT_LEN, 0x01, 14},
    {KPHC_CMP_PRE_CHUNK_LEN, 0x07, 11},
    {KPHC_CMP_LEVEL_SYMBOL_LEN, 0x01, 10},
    {KPHC_CMP_LEVEL_LEN, 0x1F, 5},
    {KPHC_CMP_ALGORITHM_LEN, 0x07, 2},
    {KPHC_CMP_CHUNK_SIZE_LEN, 0x03, 0}};

typedef struct relFileCompressOption {
    unsigned byteConvert : KPHC_CMP_BYTE_CONVERT_LEN;
    unsigned diffConvert : KPHC_CMP_DIFF_CONVERT_LEN;
    unsigned compressPreallocChunks : KPHC_CMP_PRE_CHUNK_LEN;
    unsigned compressLevelSymbol : KPHC_CMP_LEVEL_SYMBOL_LEN;
    unsigned compressLevel : KPHC_CMP_LEVEL_LEN;
    unsigned compressAlgorithm : KPHC_CMP_ALGORITHM_LEN;
    unsigned compressChunkSize : KPHC_CMP_CHUNK_SIZE_LEN;
}KPHCRelFileCompressOption;

const int KPHC_CFS_EXTENT_SIZE = 129;
const int KPHC_CFS_LOGIC_BLOCKS_PER_EXTENT = KPHC_CFS_EXTENT_SIZE - 1;
const int KPHC_CFS_EXTENT_COUNT_PER_FILE = RELSEG_SIZE / KPHC_CFS_EXTENT_SIZE;
const int KPHC_CFS_MAX_BLOCK_PER_FILE = KPHC_CFS_EXTENT_COUNT_PER_FILE * KPHC_CFS_EXTENT_SIZE;
const int KPHC_EXTENT_OPEN_FILE = 0;
const int KPHC_WRITE_BACK_OPEN_FILE = 1;
const int KPHC_EXTENT_CREATE_FILE = 2;

/* md storage manager functionality */
extern void mdinit(void);
extern void mdopen(SMgrRelation reln);
extern void mdclose(SMgrRelation reln, ForkNumber forknum);
extern void mdcreate(SMgrRelation reln, ForkNumber forknum, bool isRedo);
extern bool mdexists(SMgrRelation reln, ForkNumber forknum);
extern void mdunlink(RelFileNodeBackend rnode, ForkNumber forknum, bool isRedo);
extern void mdextend(SMgrRelation reln, ForkNumber forknum,
					 BlockNumber blocknum, char *buffer, bool skipFsync);
extern bool mdprefetch(SMgrRelation reln, ForkNumber forknum,
					   BlockNumber blocknum);
extern void mdread(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum,
				   char *buffer);
extern void mdwrite(SMgrRelation reln, ForkNumber forknum,
					BlockNumber blocknum, char *buffer, bool skipFsync);
extern void mdwriteback(SMgrRelation reln, ForkNumber forknum,
						BlockNumber blocknum, BlockNumber nblocks);
extern BlockNumber mdnblocks(SMgrRelation reln, ForkNumber forknum);
extern void mdtruncate(SMgrRelation reln, ForkNumber forknum,
					   BlockNumber nblocks);
extern void mdimmedsync(SMgrRelation reln, ForkNumber forknum);

extern void ForgetDatabaseSyncRequests(Oid dbid);
extern void DropRelationFiles(RelFileNode *delrels, int ndelrels, bool isRedo);

/* md sync callbacks */
extern int	mdsyncfiletag(const FileTag *ftag, char *path);
extern int	mdunlinkfiletag(const FileTag *ftag, char *path);
extern bool mdfiletagmatches(const FileTag *ftag, const FileTag *candidate);

#endif/* MD_H */
